// pages/register/register.js
import WxValidate from '../../utils/WxValidate'
import {
  getSMS,
  registerAccount
} from '../../apis/api'
import crypt from '../../utils/crypt'
var guiCode = require('../../utils/guiCode.js');
var md5 = require('md5')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    smsFlag: false,
    snsMsgWait: 60,
    sendTime: '获取验证码',
    rePass: '',
    checked: '',
    disabled:true,
    loginForm: {
      telephone: '',
      password: '',
      repassword: '',
      code: '',
      username: 'user'
    }
  },
  loginFormPhone: function (e) {
    this.setData({
      ['loginForm.telephone']: e.detail.value
    })
  },

  loginFormPassword: function (e) {
    this.setData({
      ['loginForm.password']: e.detail.value
    })
  },

  loginFormRePassword: function (e) {
    this.setData({
      ['loginForm.repassword']: e.detail.value
    })
  },

  loginFormVercode: function (e) {
    this.setData({
      ['loginForm.code']: e.detail.value
    })
  },
  loginFormRePasswordBlur: function (e) {
    if (e.detail.value !== this.data.loginForm.password) {
      wx.showModal({
        content: '两次输入密码不一致',
        showCancel: false
      })
      this.setData({
        rePass: false
      })
    } else {
      this.setData({
        rePass: true
      })
    }
  },
  loginFormRePasswordBlur0: function (e) {
    if (e.detail.value !== this.data.loginForm.repassword) {
      this.setData({
        rePass: false
      })
    } else {
      this.setData({
        rePass: true
      })
    }
  },
  showModal(err) {
    wx.showModal({
      content: err.msg,
      showCancel: false
    })
  },
  initValidate() {
    const rules = {
      telephone: {
        required: true,
        tel: true
      },
      password: {
        required: true
      },
      repassword: {
        required: true
      },
      code: {
        required: true
      }
    }
    const messages = {
      telephone: {
        required: '请输入手机号',
        tel: '请输入正确的手机号'
      },
      password: {
        required: '请输入密码'
      },
      repassword: {
        required: '请再次输入密码'
      },
      code: {
        required: '请输入验证码'
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  checkboxChange:function(e){
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    let val = e.detail.value
    if(val.length>0){
      this.setData({
        checked:true,
        disabled:false
      })
    }else{
      this.setData({
        checked:false,
        disabled:true
      })
    }
  },
    // 跳转页面 隐私协议
    agreement:function(){
      wx.navigateTo({
        url: '/pages/Agreement/Agreement'
      })
    },
  formSubmit: async function (e) {
    const params = e.detail.value
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false
    }
    
    
    if (!this.data.rePass) {
      wx.showModal({
        content: '两次输入密码不一致',
        showCancel: false
      })
      return false
    }
    const res = await registerAccount({
      telephone: this.data.loginForm.telephone,
      password: crypt.encrypt(md5(this.data.loginForm.password)),
      repassword: crypt.encrypt(md5(this.data.loginForm.password)),
      code: this.data.loginForm.code,
      username: 'user'
    })
    console.log(res);
    if (res.code !== 200) {
      return wx.showModal({
        content: res.msg,
        showCancel: false
      })
    }
    wx.showToast({
      title: '注册成功',
      icon: 'success',
      duration: 1000
    })
    setTimeout(() => {
      wx.navigateTo({
        url: '/pages/tolog/tolog',
      })
    }, 1000)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initValidate()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  async sendCode() {
    // 60秒后重新获取验证码
    if (!this.data.smsFlag) {
      const res = await getSMS({
        telephone: this.data.loginForm.telephone
      })
      if (res.code !== 200) return wx.showModal({
        content: res.msg,
        showCancel: false
      })
      let inter = setInterval(function () {
        this.setData({
          smsFlag: true,
          sendTime: this.data.snsMsgWait + 's后重发',
          snsMsgWait: this.data.snsMsgWait - 1
        });
        if (this.data.snsMsgWait < 0) {
          clearInterval(inter)
          this.setData({
            sendTime: '获取验证码',
            snsMsgWait: 60,
            smsFlag: false
          });
        }
      }.bind(this), 1000);
    }
  }
})